/*
 *  Queen Mary University of London MOM System
 *  
 *  Copyright Queen Mary University of London
 *  
 *  Authors:
 *  Bob Chew (bob.chew@elec.qmul.ac.uk)
 *  Beatriz Viñal Murciano (beavimu@gmail.com)
 *  John Bigham (john.bigham@elec.qmul.ac.uk)
 *  
 *  
 *  Portions of this software uses Apache Qpid
 *    http://qpid.apache.org
 *  
 *  Apache Qpid license:
 * --------------------------------------------------------------
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * -------------------------------------------------------------
 * 
 */
package brokerFailureDetector;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Properties;


/**
 * Creates a detector.
 */
public class DetectorCreator {

    /** IP address of the broker controller. */
    private static String controllerIP;
    /**
     * List of the IP addresses of the brokers in the system mapped to the
     * ports in which they are listening.
     */
    private static HashMap<String, Integer> brokerAddresses
            = new HashMap<String, Integer>();

    /**
     * Create a detector.
     */
    public static void main(String[] args) {
        String hostIP = "";
        if (args.length == 0) {
            throw new IllegalArgumentException(
                    "You must specify a file to read properties from");
        } else {
            String propFileName = args[0];
            //read properties from properties file
            try {
                FileInputStream fist = new FileInputStream(propFileName);
                Properties topologyProps = new Properties();
                topologyProps.loadFromXML(fist);
                controllerIP = topologyProps.getProperty("controllerIP");
                hostIP = topologyProps.getProperty("hostIP");
                int numBrokers = Integer.parseInt(
                        topologyProps.getProperty("numBrokers"));
                for (int i = 1; i <= numBrokers; i++) {
                    String ip = "broker" + i + "IP";
                    String port = "broker" + i + "port";
                    String brokerIP = topologyProps.getProperty(ip);
                    int brokerPort
                            = Integer.parseInt(topologyProps.getProperty(port));
                    brokerAddresses.put(brokerIP, brokerPort);
                }
                System.out.println("Controller IP: " + controllerIP);
                System.out.println("Brokers IPs: " + brokerAddresses);
                System.out.println("Host broker:" + hostIP);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        new Detector(hostIP, controllerIP, brokerAddresses);
    }

}
